package com.jcrj.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Blob;
import java.util.Date;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Hibernate;

public class FileIo {
	public static synchronized Blob getFile(String fileName) throws IOException {
		FileInputStream fis = null;
		Blob blob = null;
		if ((fileName == null) || (fileName.equals(""))) {
			return null;
		}
		File file = new File(fileName);
		fis = new FileInputStream(file);
		blob = Hibernate.createBlob(fis);

		return blob;
	}

	public static byte[] getBytesFromFile(File file) {
		byte[] ret = (byte[]) null;
		FileInputStream in = null;
		ByteArrayOutputStream out = null;
		try {
			if (file != null) {
				in = new FileInputStream(file);
				out = new ByteArrayOutputStream(1024);
				byte[] b = new byte[1024];
				int n;
				while ((n = in.read(b)) != -1) {
					// int n;
					out.write(b, 0, n);
				}
				ret = out.toByteArray();
			}
		} catch (IOException e) {
			e.printStackTrace();
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	public static String getContentToString(byte[] content) {
		String ret = null;
		ByteArrayOutputStream out = null;
		try {
			if (content != null) {
				out = new ByteArrayOutputStream(1024);
				out.write(content);
				ret = out.toString();
			}
		} catch (IOException e) {
			e.printStackTrace();
			try {
				if (out != null)
					out.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	public static String fileUpload(File file, String fileName, String account) {
		String path = SysConfig.getInstance().getProperty("filepath");
		String temp = account + "/";
		String root = path + "/" + temp;
		String newPath = null;
		InputStream is = null;
		OutputStream os = null;
		File deskFile = null;
		String newFile = "";
		try {
			File filepath = new File(root);
			if (!filepath.exists()) {
				filepath.mkdir();
			}
			is = new FileInputStream(file);
			String name = fileName.substring(fileName.lastIndexOf("."), fileName.length());
			newFile = new Date().getTime() + name;
			deskFile = new File(root, newFile);
			os = new FileOutputStream(deskFile);
			byte[] bytefer = new byte[1024];
			int length = 0;
			while ((length = is.read(bytefer)) > 0) {
				os.write(bytefer, 0, length);
			}
			newPath = temp + newFile;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (os != null) {
					os.close();
				}
				if (is != null)
					is.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return newPath;
	}

	public static void downloadLocal(HttpServletResponse response, String path, String name) {
		String root = SysConfig.getInstance().getProperty("filepath");
		String rootFile = root + "/" + path;

		File file = new File(rootFile);
		if (file.exists()) {
			InputStream inStream = null;
			try {
				inStream = new FileInputStream(file);
				response.setCharacterEncoding("UTF-8");
				response.reset();
				response.setContentType("bin");
				response.addHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(name, "UTF-8") + "\"");

				byte[] b = new byte[1000];
				int data;
				while ((data = inStream.read(b)) > 0) {
					// int data;
					response.getOutputStream().write(b, 0, data);
				}
			} catch (Exception e) {
				System.out.println("取消下载");

				if (inStream == null)
					return;
				try {
					inStream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			} finally {
				if (inStream != null)
					try {
						inStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
		} else {
			try {
				response.setCharacterEncoding("GBK");
				response.getWriter().write("<script type=\"text/javascript\">alert(\"文件不存在！\");window.history.back();</script>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void downloadLocal(HttpServletResponse response, File file, String name) {
		if (file.exists()) {
			InputStream inStream = null;
			try {
				inStream = new FileInputStream(file);
				response.setCharacterEncoding("UTF-8");
				response.reset();
				response.setContentType("bin");
				response.addHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(name, "UTF-8") + "\"");

				byte[] b = new byte[1000];
				int data;
				while ((data = inStream.read(b)) > 0) {
					// int data;
					response.getOutputStream().write(b, 0, data);
				}
			} catch (Exception e) {
				System.out.println("取消下载");

				if (inStream == null)
					return;
				try {
					inStream.close();
					file.delete();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			} finally {
				if (inStream != null)
					try {
						inStream.close();
						file.delete();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
			try {
				inStream.close();
				file.delete();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				response.setCharacterEncoding("GBK");
				response.getWriter().write("<script type=\"text/javascript\">alert(\"文件不存在！\");window.history.back();</script>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.util.FileIo JD-Core Version: 0.6.0
 */